
<template>
  <!-- 物料选择 -->
  <div class="material-select">
    <!-- top -->
    <div class="top">
      <div class="top-left">
        <div class="info">
          <div class="tabs-title">
            <span style="margin-left: 20px; color: #2e61d7; font-weight: bold"
              >数据</span
            >
          </div>
        </div>
        <div class="ipt-box">
            <input class="ipt2" type="text" placeholder="输入项目编号" />
            <span
          ><img class="img" src="@/assets/search.png" alt="点击搜索"
        /></span>
        </div>
      </div>
      <div class="top-center"></div>
      <div class="top-right">
        <input class="ipt3" type="text" placeholder="输入项目编号" />
            <span
          ><img class="img" src="@/assets/search.png" alt="点击搜索"
        /></span>
      </div>
    </div>
    <!-- content -->
    <div class="content">
      <div class="left">
        <div class="e-tree-file">
          <div class="name">设备类别</div>

          <el-tree
            :data="data"
            :props="defaultProps"
            @node-click="handleNodeClick"
          ></el-tree>
        </div>
      </div>
      <div class="right">
        <div class="right-top e-table">
            <el-table
                ref="initTable"
                border
                :data="tableData"
                :row-style="rowClass"
                @row-dblclick="checkRow"
                @select="changeRow"
                v-loading="loading"
                @select-all="changeRow"
            >
                <el-table-column type="selection" v-if="multiple"></el-table-column>
                <el-table-column align="center" label="设备名称"  prop="equipmentName"></el-table-column>
                <el-table-column align="center" label="计量单位"  prop="unit"></el-table-column>
            </el-table>

        </div>
        <ComPagination
                :total="pagination.total"
                @update:currentPage="currentChange"
                :currentPage="pagination.page"
                @update:pageSize="updatePageSize"
            />
        <div class="right-bottom ">
          <div class="tabs-title">
            <span style="margin-left: 20px; color: #2e61d7; font-weight: bold"
              >已选</span
            >
          </div>
          <div class="table-box  e-table">
             <el-table
                border
                :data="tableData1"
                @row-dblclick="cancelRow"
            >
                <el-table-column align="center" label="设备名称"  prop="equipmentName"></el-table-column>
                <el-table-column align="center" label="计量单位"  prop="unit"></el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </div>
    <!-- footer -->
    <div class="footer">
      <div class="left"></div>
      <div class="right">
        双击选择或取消数据
        <el-button type="mini" @click="confirm">确定</el-button>
        <el-button type="mini" class="colse-btn" @click="cancel">取消</el-button>
      </div>
    </div>
  </div>
</template>
<script>
import ComPagination from '@/components/pagination/pagination.vue'
import request from '@/api/equip/equipPlan.js'
export default {
    components: {
        ComPagination,
    },
    props: {
        multiple: {  //是否支持多选，true为多选false为单选
            type: Boolean,
            default: true,
        }
    },
    data () {
        return {
            data: [],
            loading: false,
            pagination: {
                'categoryId': '',
                'limit': 10,
                'page': 1,
                'query': '',
                'totle': 0
            },
            defaultProps: {
                children: 'childList',
                label: 'categoryName',
            },
            tableData: [], //左边table的数据，虚从接口获取
            tableData1: [], //右边table选择的数据
        }
    },
    created () {
        this.getEquipment()
    },
    mounted () {
        request.getEquipmentCategory().then(res=>{
            this.data = res
        })
    },
    methods: {
        handleNodeClick (row) {
            if(row.childList) return false
            this.pagination.categoryId = row.categoryId
            this.getEquipment()
        },
        currentChange (page) {
            console.log('page', page)
            this.pagination.page = page
            this.getEquipment()
        },
        updatePageSize (limit) {
            console.log('limit', limit)
            this.pagination.limit = limit
            this.getEquipment()
        },
        getEquipment () {
            this.loading = true
            request.getEquipment(this.pagination).then(res=>{
                this.tableData = res.list
                this.pagination.totle = res.totalCount
                this.$nextTick(()=>{
                    let initTable =  this.$refs.initTable
                    initTable.clearSelection()
                    this.tableData1.forEach(item=>{
                        this.tableData.forEach(row=>{
                            if(row.equipmentId === item.equipmentId) {
                                console.log('row', row)
                                initTable.toggleRowSelection(row, true)
                            }
                        })
                    })
                })
                this.loading = false
            })
        },
        //左边表格双击选中
        checkRow (row) {
            if(this.multiple) {
                console.log('row', row)
                let obj =  this.tableData1.find(x=>x.equipmentId === row.equipmentId )
                if(!obj) {
                    this.tableData1.push(row)
                    this.$nextTick(()=>{
                        this.tableData1.forEach(item=>{
                            this.$refs.initTable.toggleRowSelection(item, true)
                        })
                    })
                }

            }
            else{
                this.tableData1 = [ row]
            }
        },
        //对所选行进行高亮显示
        rowClass ({ row }) {
            let stylejson = {}
            this.tableData1.forEach(item=>{
                if(item.equipmentId === row.equipmentId) { //暂时用equipName字段进行标识，根据实际返回字段换成其他的
                    stylejson.backgroundColor = '#ecf5ff'
                }
            })
            return stylejson
        },
        //左边表格选框选中
        changeRow (selection) {
            if(this.multiple) {
                console.log('selection', selection)
                selection.forEach(item=>{
                    let arr = this.tableData1.find(x=>x.equipmentId === item.equipmentId) //判断在右边表格里是否有左边已选择的数据
                    if(!arr) {
                        this.tableData1.push(item)  //没有则添加进去
                    }
                })
                //下面是判断左边表格取消操作，常规的赋值会让翻页后数据出现问题，无法保留其他页面的选择
                this.tableData.forEach(item=>{
                    let obj = selection.find(x=>x.equipmentId === item.equipmentId) //查找到取消的那一条
                    if(!obj) {
                        let index =  this.tableData1.findIndex(x=>x.equipmentId === item.equipmentId) //看取消的那一条是否在右边表格里
                        if(index !== -1) {
                            this.tableData1.splice(index, 1) //在的话就删除右边表格的这一条
                        }
                    }

                })
            }
        },
        //取消选择
        cancelRow (row) {
            this.tableData1.forEach((item, index)=>{
                if(row.equipmentId === item.equipmentId) {
                    this.tableData1.splice(index, 1)
                    this.$refs.initTable.toggleRowSelection(item, false)
                }else{
                    this.$refs.initTable.toggleRowSelection(item, true)
                }
            })
        },
        cancel () {
            this.$emit('cancel')
        },
        confirm () {
            this.$emit('confirm', this.tableData1)
        }
    },
}
</script>
<style lang="scss" scoped>
.material-select {
  width: 80%;
  height: 80%;
  display: flex;
  margin: auto;

  flex-direction: column;
  .top {
    height: 50px;
    // border: 1px solid #000;
    line-height: 50px;
    display: flex;

    .top-left {
      width: 300px;

      display: flex;
      .info {
        width: 50px;
        position: relative;
        .tabs-title::before {
          content: "";
          height: 22px;
          width: 8px;
          border-radius: 40px;
          background-color: #2e61d7;
          display: block;
          position: absolute;
          left: 0px;
          top: 14px;
        }
      }
      .ipt-box {
        flex: 1;
        position: relative;
        .ipt2 {
      height: 23px;
      border-radius: 5px;

      margin-bottom: 15px;
      margin-left: 35px;
      cursor: pointer;
      border: 1px solid #ccc;
    }
    .img {
      width: 15px;
      height: 15px;
      position: absolute;
      top: 19px;
      right: 62px;
      cursor: pointer;
    }
    ::placeholder {
      padding-left: 10px;
      color: #cccc;
    }
      }
    }
    .top-center {
      flex: 2;
    }
    .top-right {
      flex: 1;
      position: relative;
      .ipt3 {
      height: 23px;
      border-radius: 5px;
      margin-top: 15px;
      margin-bottom: 15px;
      margin-left: 60px;
      cursor: pointer;
      border: 1px solid #ccc;

    }

    img {
      width: 15px;
      height: 15px;
      position: absolute;
      top: 20px;
      right: 20px;
      cursor: pointer;
    }
    ::placeholder{
       color: #cccc;
      padding-left: 10px;
    }
    }

  }
  .content {
    flex: 1;
    margin-top: 10px;
    display: flex;

    .left {
      width: 280px;

      overflow-y: auto;
      margin-right: 20px;
      border-radius: 5px;
      position: relative;
      box-sizing: border-box;
      border: 1px solid #ccc;
      .name {
        height: 40px;
        line-height: 40px;
        text-align: center;
        font-weight: 700;

        background-color: rgb(224, 231, 249);
      }
    }
    .right {
      flex: 1;
      border-radius: 5px;
      //  border: 1px solid #ccc;
      display: flex;
      flex-direction: column;
      .right-top {
        flex: 1;

        .table-box {
          width: 100%;
          ::v-deep.has-gutter th {
            background: #e0e7f9 !important;
            color: #000;
          }
          ::v-deep.el-table--enable-row-hover .el-table__body tr:hover {
            font-weight: bold;
          }
          ::v-deep.el-table ::v-deep .el-table__expand-icon--expanded {
            transform: none;
          }
          ::v-deep.el-tree ::v-deep .el-tree-node__expand-icon.expanded {
            -webkit-transform: rotate(0deg);
            transform: rotate(0deg);
          }
          ::v-deep.cell {
            display: flex;
            align-items: center;
          }
        }
        ::v-deep.e-pagination {
          margin-top: 5px !important;
        }
      }
      .right-bottom {
        flex: 1;
        border-radius: 5px;

        margin-top: 10px;
        position: relative;
        .tabs-title {
          margin-bottom: 10px;
        }
        .tabs-title::before {
          content: "";
          height: 22px;
          width: 8px;
          border-radius: 40px;
          background-color: #2e61d7;
          display: block;
          position: absolute;
          left: 0px;
          top: -2px;
        }
        ::v-deep.has-gutter th {
          background: #e0e7f9 !important;
          color: #000;
        }
        ::v-deep.el-table--enable-row-hover .el-table__body tr:hover {
          font-weight: bold;
        }
        ::v-deep.el-table ::v-deep .el-table__expand-icon--expanded {
          transform: none;
        }
        ::v-deep.el-tree ::v-deep .el-tree-node__expand-icon.expanded {
          -webkit-transform: rotate(0deg);
          transform: rotate(0deg);
        }

      }
    }
  }
  .footer {
    height: 50px;
    margin-top: 20px;
    display: flex;
    background-color: #eff2f6;
    border: 1px solid #ccc;
    .left {
      width: 70%;
      height: 50px;
    }
    .right {
      line-height: 50px;
      font-weight: 500;
      color: rgb(233, 84, 66);
      font-size: 14px;
      .colse-btn {
        background-color: #fff;
        color: #000;
      }
    }
    ::v-deep.el-button--mini {
      width: 60px;
      color: #fff;
      margin-left: 10px;
      background-color: rgb(46, 97, 215);
    }
  }
}
</style>

